package com.hellogood.work.evaluate.server.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hellogood.work.evaluate.server.db.DBHelper;
import com.hellogood.work.evaluate.shared.exception.DatabaseException;
import com.hellogood.work.evaluate.shared.po.Customer;

public class CustomerDAO extends DBHelper {

	@Override
	public List<?> initializeObject(ResultSet rs) throws SQLException {
		List<Customer> list = new ArrayList<Customer>();
		if (rs != null) {
			while (rs.next()) {
				Customer customer = new Customer();
				String custNo = rs.getString("custcode");
				String custName = rs.getString("custName");
				String proCode = rs.getString("ProCode");
				String proName = rs.getString("ProName");
				String custAdd = rs.getString("CustAdd");
				String phone = rs.getString("Phone");
				String serverIp = rs.getString("ServerIp");
				String domain = rs.getString("Domain");
				String contact = rs.getString("Contact");
				String mark = rs.getString("Mark");

				customer.setCustomerNo(custNo == null ? "" : custNo.trim());
				customer.setCustomerName(custName == null ? "" : custName.trim());
				customer.setProductCode(proCode == null ? "" : proCode.trim());
				customer.setProductName(proName == null ? "" : proName.trim());
				customer.setAddress(custAdd == null ? "" : custAdd.trim());
				customer.setTelephone(phone == null ? "" : phone.trim());
				customer.setServerIP(serverIp == null ? "" : serverIp.trim());
				customer.setDomain(domain == null ? "" : domain.trim());
				customer.setHasVPN(rs.getBoolean("vpn"));
				customer.setContact(contact == null ? "" : contact.trim());
				customer.setRemark(mark == null ? "" : mark.trim());
				list.add(customer);
			}
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Customer> getList() {
		String sql = "SELECT * FROM HGCust ORDER BY CustCode";
		List<?> list = super.query(sql);
		return (List<Customer>) list;
	}

	public Customer getCustomer(String code) {
		String sql = "SELECT * FROM HGCust WHERE CustCode = '" + code + "'";
		List<?> list = super.query(sql);
		if (list != null && list.size() > 0) {
			return (Customer) list.get(0);
		}
		return null;
	}

	public String modify(Customer cust, boolean isModiy) {
		int vpn = cust.isHasVPN() ? 1 : 0;
		String sql = "INSERT INTO HGCust(custcode,custname,procode,proname,custadd,phone,serverip,domain,vpn,contact,mark) VALUES('"
				+ cust.getCustomerNo() + "','" + cust.getCustomerName() + "','" + cust.getProductCode() + "','" + cust.getProductName() + "','"
				+ cust.getAddress() + "','" + cust.getTelephone() + "','" + cust.getServerIP() + "','" + cust.getDomain() + "'," + vpn + ",'"
				+ cust.getContact() + "','" + cust.getRemark() + "')";
		if (isModiy) {
			sql = "UPDATE HGCust SET custname = '" + cust.getCustomerName() + "',procode = '" + cust.getProductCode() + "',proname = '"
					+ cust.getProductName() + "',custadd = '" + cust.getAddress() + "',phone = '" + cust.getTelephone() + "'," + "serverip = '"
					+ cust.getServerIP() + "',domain = '" + cust.getDomain() + "',vpn = " + vpn + ",contact = '" + cust.getContact() + "',mark = '"
					+ cust.getRemark() + "' WHERE custcode = '" + cust.getCustomerNo() + "'";
		}
		try {
			if (super.execute(sql) > 0) {
				return "";
			}
		} catch (DatabaseException e) {
			e.printStackTrace();
			return "执行新建、修改客户信息错误";
		}
		return "执行新建、修改客户信息错误";
	}

}
